package com.estudy.service;

import com.alibaba.fastjson.JSON;
import com.estudy.bean.EmployeeDO;
import com.estudy.bean.EmployeeDOExample;
import com.estudy.dao.EmployeeDOMapper;
import com.estudy.entity.RoleEnum;
import com.estudy.entity.User;
import com.google.common.collect.Lists;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author: yi.jianhua
 * @create: 2019/08/02 18:19
 */
@Service
public class UserService {
    private static final Logger logger = Logger.getLogger(UserService.class);
    @Autowired
    private EmployeeDOMapper mapper;

    /**
     * 查询所有的用户
     */
    public List<User> queryAllUsers() {
        EmployeeDOExample employeeDOExample = new EmployeeDOExample();
        List<EmployeeDO> employeeDOS = mapper.selectByExample(employeeDOExample);
        return convert2UserList(employeeDOS);
    }

    private List<User> convert2UserList(List<EmployeeDO> employeeDOS) {
        if (CollectionUtils.isEmpty(employeeDOS)) {
            return Collections.emptyList();
        }
        List<User> result = Lists.newArrayListWithCapacity(employeeDOS.size());
        for (EmployeeDO employeeDO : employeeDOS) {
            User user = convet2User(employeeDO);
            result.add(user);
        }
        return result;
    }

    private User convet2User(EmployeeDO employeeDO) {
        User user = new User();
        user.setId(String.valueOf(employeeDO.getId()));
        user.setRoleCode(employeeDO.getEmployee_role());
        RoleEnum enumByCode = RoleEnum.getEnumByCode(employeeDO.getEmployee_role());
        user.setRole(enumByCode == null ? "" : enumByCode.getName());
        user.setNickName(employeeDO.getEmployee_alias());
        user.setStatus(employeeDO.getEmployee_status());
        user.setUserName(employeeDO.getEmployee_no());
        user.setRegTime(employeeDO.getEmployee_regtime());
        user.setEmail(employeeDO.getEmployee_email());
        return user;
    }

    /**
     * 更新用户的状态
     *
     * @param uid    用户id
     * @param status 用户的状态
     */
    public void updateUserStatus(String uid, int status) {
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(Integer.parseInt(uid));
        employeeDO.setEmployee_status(status);
        mapper.updateByPrimaryKeySelective(employeeDO);
    }

    /**
     * 添加或者更新（根据是否有UID 去判断）
     *
     * @param user
     * @return
     */
    public boolean addOrSave(User user) {
        logger.error("addOrSave user:" + JSON.toJSONString(user));
        String id = user.getId();
        if (StringUtils.isEmpty(id)) {
            EmployeeDO newEm = convert2Em(user);
            newEm.setEmployee_regtime(System.currentTimeMillis());
            mapper.insertSelective(newEm);
        } else {
            EmployeeDO employeeDO1 = convert2Em(user);
            employeeDO1.setEmployee_regtime(null);
            mapper.updateByPrimaryKeySelective(employeeDO1);
        }
        return true;
    }

    private EmployeeDO convert2Em(User user) {
        EmployeeDO result = new EmployeeDO();
        result.setEmployee_status(user.getStatus());
        if (!StringUtils.isEmpty(user.getId())) {
            result.setId(Integer.valueOf(user.getId()));
        }
        result.setEmployee_alias(user.getNickName());
        result.setEmployee_email(user.getEmail());
        result.setEmployee_pwd(user.getPass());
        result.setEmployee_regtime(user.getRegTime());
        result.setEmployee_no(user.getUserName());
        result.setEmployee_role(user.getRoleCode());
        return result;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    public boolean del(String id) {
        mapper.deleteByPrimaryKey(Integer.valueOf(id));
        return true;
    }

    /**
     * 根据Uid 查找用户
     *
     * @param uid
     * @return
     */
    public User queryUserById(String uid) {
        EmployeeDO employeeDO = mapper.selectByPrimaryKey(Integer.valueOf(uid));
        return convet2User(employeeDO);
    }

    public User getUser(String userName, String pwd) {
        EmployeeDOExample employeeDOExample = new EmployeeDOExample();
        EmployeeDOExample.Criteria criteria = employeeDOExample.createCriteria();
        criteria.andEmployee_noEqualTo(userName);
        criteria.andEmployee_pwdEqualTo(pwd);
        List<EmployeeDO> employeeDOS = mapper.selectByExample(employeeDOExample);
        if (CollectionUtils.isEmpty(employeeDOS)) {
            return null;
        }
        return convert2UserList(employeeDOS).get(0);
    }
}
